//
//  UITableViewExtension.swift
//  MT
//
//  Created by zhang qiang on 2018/12/18.
//  Copyright © 2019年 张强. All rights reserved.
//

import Foundation
import UIKit


public extension UITableView {
    func scrollToBottom(animated: Bool) {
        let bottomOffset: CGPoint = CGPoint(x: 0, y: self.verticalOffset())
        self.beginUpdates()
        self.setContentOffset(bottomOffset, animated: animated);
        self.endUpdates()
    }
    
    private func verticalOffset() -> CGFloat {
        let viewHeight: CGFloat = self.bounds.size.height;
        let contentHeight: CGFloat = self.contentSize.height;
        let topInset: CGFloat = self.contentInset.top;
        let bottomInset: CGFloat = self.contentInset.bottom;
        let bottomOffset = floorf(Float(contentHeight - bottomInset - topInset - viewHeight));
        
        return CGFloat(max(bottomOffset, 0))
    }
}
